

ces1 <- fread("raw_data/CES20_YLS_OUTPUT_vv.tab", sep = "\t",
              select = c("caseid", "teamweight",
                         "YLS390", "YLS490")) %>% 
  filter(!is.na(YLS390),
         # !is.na(YLS490),
         YLS390 <= 4,
         YLS490 <= 4 | is.na(YLS490)) %>% 
  mutate(across(c("YLS390", "YLS490"), ~ case_when(. == 1 ~ 1,
                                                   . == 2 ~ (2/3),
                                                   . == 3 ~ (1/3),
                                                   . == 4 ~ 0)),
         team = "YLS") %>% 
  rename(pre = YLS390,
         post = YLS490)

ces2 <- fread("raw_data/CES20_UTB_OUTPUT_vv.tab", sep = "\t",
              select = c("caseid", "teamweight",
                         "UTB402", "UTB403", "UTB312")) %>% 
  mutate(UTB402 = ifelse(UTB402 == 9, UTB403, UTB402)) %>% 
  select(-UTB403) %>% 
  filter(!is.na(UTB312),
         # !is.na(UTB402),
         UTB312 < 4,
         UTB402 < 4 | is.na(UTB402)) %>% 
  mutate(across(c("UTB312", "UTB402"), ~ case_when(. == 1 ~ 1,
                                                   . == 2 ~ .5,
                                                   . == 3 ~ 0)),
         team = "UTB") %>% 
  rename(pre = UTB312,
         post = UTB402)

ces3 <- fread("raw_data/CCES20_UGA_OUTPUT_vv.tab", sep = "\t",
              select = c("caseid", "teamweight",
                         "UGA500", "UGA1100")) %>% 
  filter(!is.na(UGA500),
         # !is.na(UGA1100),
         UGA500 <= 5,
         UGA1100 <= 5 | is.na(UGA1100)) %>% 
  mutate(across(c("UGA500", "UGA1100"), ~ case_when(. == 1 ~ 0,
                                                   . == 2 ~ .25,
                                                   . == 3 ~ 0.5,
                                                   . == 4 ~ 0.75,
                                                   . == 5 ~ 1)),
         team = "UGA") %>% 
  rename(pre = UGA500,
         post = UGA1100)

ces4 <- fread("raw_data/CES20_UCR_OUTPUT_vv.tab", sep = "\t",
              select = c("caseid", "teamweight",
                         "UCR306_1", "UCR306_2",
                         "UCR410_1", "UCR410_2")) %>% 
  filter(!is.na(UCR306_1),
         # !is.na(UCR410_1),
         !is.na(UCR306_2),
         # !is.na(UCR410_2),
         UCR306_1 <= 5,
         UCR410_1 <= 5 | is.na(UCR410_1),
         UCR306_2 <= 5,
         UCR410_2 <= 5 | is.na(UCR410_2)) %>% 
  mutate(across(c("UCR306_1", "UCR306_2",
                  "UCR410_1", "UCR410_2"), ~ case_when(. == 1 ~ 0,
                                                    . == 2 ~ .25,
                                                    . == 3 ~ 0.5,
                                                    . == 4 ~ 0.75,
                                                    . == 5 ~ 1)),
         team = "UCR",
         UCR306_1 = (UCR306_1 + UCR306_2) / 2,
         UCR410_1 = (UCR410_1 + UCR410_2) / 2) %>% 
  rename(pre = UCR306_1,
         post = UCR410_1) %>% 
  select(-ends_with("_2"))

ces5 <- fread("raw_data/CES20_RCO_OUTPUT_vv.tab", sep = "\t",
              select = c("caseid", "teamweight",
                         "RCO313a", "RCO313b", "RCO404a", "RCO404b")) %>% 
  mutate(RCO313a = ifelse(RCO313a == 9, RCO313b, RCO313a),
         RCO404a = ifelse(RCO404a == 9, RCO404b, RCO404a)) |> 
  filter(!is.na(RCO313a),
         # !is.na(RCO404a),
         RCO313a <= 4,
         RCO404a <= 4 | is.na(RCO404a)) %>% 
  mutate(across(c("RCO313a", "RCO404a"), ~ case_when(. == 1 ~ 1,
                                                   . == 2 ~ (2/3),
                                                   . == 3 ~ (1/3),
                                                   . == 4 ~ 0)),
         team = "RCO") %>% 
  rename(pre = RCO313a,
         post = RCO404a) |> 
  select(-RCO313b, -RCO404b)

ces6 <- fread("raw_data/CES20_MCS_OUTPUT_vv.tab", sep = "\t",
              select = c("caseid", "teamweight",
                         "MCS304A", "MCS304B", "MCS416")) %>% 
  mutate(MCS304A = ifelse(MCS304A == 9, MCS304B, MCS304A)) %>% 
  filter(!is.na(MCS304A),
         # !is.na(MCS416),
         MCS304A <= 4,
         MCS416 <= 4 | is.na(MCS416)) %>% 
  mutate(across(c("MCS304A", "MCS416"), ~ case_when(. == 1 ~ 1,
                                                     . == 2 ~ (2/3),
                                                     . == 3 ~ (1/3),
                                                     . == 4 ~ 0)),
         team = "MCS") %>% 
  rename(pre = MCS304A,
         post = MCS416) %>% 
  select(-MCS304B)

ces7 <- fread("raw_data/CES20_LSU_OUTPUT_vv.tab", sep = "\t",
              select = c("caseid", "weight",
                         "LSU352", "LSU418")) %>% 
  filter(!is.na(LSU352),
         # !is.na(LSU418),
         LSU352 <= 4,
         LSU418 <= 4 | is.na(LSU418)) %>% 
  mutate(across(c("LSU352", "LSU418"), ~ case_when(. == 1 ~ 1,
                                                    . == 2 ~ (2/3),
                                                    . == 3 ~ (1/3),
                                                    . == 4 ~ 0)),
         team = "LSU") %>% 
  rename(pre = LSU352,
         post = LSU418,
         teamweight = weight)

##################### 

conf <- bind_rows(ces1,
                  ces2,
                  ces3,
                  ces4,
                  ces5,
                  ces6,
                  ces7)

saveRDS(conf, "temp/ces_confidence.rds")
